package zhu.common.service;

import org.springframework.core.task.TaskExecutor;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

public class MailService {
	
	private JavaMailSender sender;
	private TaskExecutor taskExecutor;

	public void sendAsyncMail(final String email) {
		taskExecutor.execute(new Runnable() {
			public void run() {
				try {
					//sendTemplateMail(userId);
					sendSimpleEmail(email);
					System.out.println("邮件发送成功！");
				} catch (Exception e) {
					System.out.println("邮件发送失败！，异常信息：" + e.getMessage());
				}
			}
		});
	}
	
	//发送纯文本邮件
	public void sendSimpleEmail(String email){
		System.out.println("here");
		
		SimpleMailMessage msg=new SimpleMailMessage();
		
		msg.setFrom("1466194536@qq.com");
		msg.setTo(email);
		msg.setSubject("注册成功");
		msg.setText("恭喜，注册成功助跑网");
		sender.send(msg);
	
	}
//	//发送HTML类型的邮件
//	public void sendHtmlMail(int userId) throws MessagingException {
//		MimeMessage msg = sender.createMimeMessage();//创建MimeMessage 
//		MimeMessageHelper helper = new MimeMessageHelper(msg, false, "utf-8");//指定编码为UTF-8，同时标识为非multiple的消息
//		helper.setFrom("1061734306@qq.com");
//		helper.setTo("1647473721@qq.com");
//		helper.setSubject("注册成功");
//		String htmlText = "<html><head>"
//				+ "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">"
//				+ "</head><body>" + "恭喜，您在宝宝淘论坛已经注册成功!您的用户ID为："
//				+ "<font size='20' size='30'>" + userId + "</font>"
//				+ "<body></html>";//构造html代码
//		helper.setText(htmlText, true);//设置邮件内容，第二个参数为true表示为HTML邮件
//		sender.send(msg);
//	}
//
//	//发送带内嵌文件的邮件
//	public void sendInlineMail(String email) throws MessagingException {
//		MimeMessage msg = sender.createMimeMessage();
//		MimeMessageHelper helper = new MimeMessageHelper(msg, true, "utf-8");//第二个参数表示为multiple类型
//		helper.setFrom("1466194536@qq.com");
//		helper.setTo(email);
//		helper.setSubject("注册成功");
//		String htmlText = "<html><head>"
//				+ "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">"
//				+ "</head><body>" + "欢迎访问宝宝淘论坛！</hr>"
//				+ "<div><img src=\"cid:img01\"></img></div>" + "<body></html>";//引用内嵌文件的特殊标识
//		helper.setText(htmlText, true);
//		System.out.println("here1");
//		ClassPathResource img = new ClassPathResource("logo.jpg");//从类路径加载图片文件
//		System.out.println("here2");
//		helper.addInline("img01", img);//添加内嵌文件
//		System.out.println("here3");
//		//helper.addInline("img01", new File("${base}/includes/images/logo.jpg"));
//		sender.send(msg);
//		System.out.println("here4");
//	}

//	//发送附件的邮件
//	public void sendAttachmentMail() throws Exception {
//		MimeMessage msg = sender.createMimeMessage();
//		MimeMessageHelper helper = new MimeMessageHelper(msg, true, "utf-8");
//		helper.setFrom("1061734306@qq.com");
//		helper.setTo("1647473721@qq.com");
//		helper.setSubject("注册成功");
//		helper.setText("欢迎访问宝宝淘论坛！");
//		ClassPathResource file1 = new ClassPathResource("bbt.zip");
//		helper.addAttachment("file01.zip", file1.getFile());
//		ClassPathResource file2 = new ClassPathResource("file.doc");
//		helper.addAttachment("file02.doc", file2.getFile());
//		sender.send(msg);
//	}
//	
//
//	//发送纯文本和HTML双版本的邮件
//	public void sendAlternativeMail() throws Exception {
//		MimeMessagePreparator mmp = new MimeMessagePreparator() {
//			public void prepare(MimeMessage msg) throws Exception {
//				MimeMessageHelper helper = new MimeMessageHelper(msg, true,
//						"utf-8");
//				helper.setFrom("1061734306@qq.com");
//				helper.setTo("1647473721@qq.com");
//				helper.setSubject("注册成功");
//
//				MimeMultipart mmPart = new MimeMultipart("alternative");
//				msg.setContent(mmPart);
//
//				BodyPart plainTextPart = new MimeBodyPart();
//				plainTextPart.setText("欢迎访问宝宝淘论坛！");
//				mmPart.addBodyPart(plainTextPart);
//
//				BodyPart htmlPart = new MimeBodyPart();
//				String htmlText = "<html><head>"
//						+ "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">"
//						+ "</head><body><font size='20' size='30'>"
//						+ "欢迎访问宝宝淘论坛</font>" + "<body></html>";
//				htmlPart.setContent(htmlText, "text/html;charset=utf-8");
//				mmPart.addBodyPart(htmlPart);
//			}
//		};
//		sender.send(mmp);
//	}
	
	
	
	public TaskExecutor getTaskExecutor() {
		return taskExecutor;
	}
	public void setTaskExecutor(TaskExecutor taskExecutor) {
		this.taskExecutor = taskExecutor;
	}
	public JavaMailSender getSender() {
		return sender;
	}
	public void setSender(JavaMailSender sender) {
		this.sender = sender;
	}
	

}
